from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time, random
from MyAutomator import MyAutomation


class Qutoutiao(MyAutomation):

    def __init__(self, APP_PACKAGE, APP_ACTIVITY):
        # 火山极速版
        self.treasure_ele = 'com.ss.android.ugc.livelite:id/ql' #ql
        super().__init__(APP_PACKAGE, APP_ACTIVITY)






    # 小视频
    def smallvideo(self, interval):
        if qutoutiao_appPackage == 'com.ss.android.ugc.livelite':
            try:
                treasure_pop = self.wait.until(EC.presence_of_element_located((By.ID, self.treasure_ele)))
                treasure_pop.click()
                print('打开短视频')
            except:
                print('不需要打开短视频')

        start = time.time()
        count = 0
        while True:
            try:
                time.sleep(random.randint(5, 12))
                self.swipeUp()
                end = start + interval
                now = time.time()
                count += 1
                print('page:'+str(count))
                print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now)))
                # print(str(interval))
                # print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end)))
                if now >= end:
                    print('结束小视频循环')
                    self.driver.press_keycode(3)
                    self.driver.close()
                    break
            except:
                self.driver.press_keycode(3)
                self.driver.close()

    def hshb(self, interval):
        time.sleep(3)
        treasure = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@text="红包"]')))
        treasure.click()
        # self.swipeUp()

        for i in range(20):
            try:
                treasure2 = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@text="领100金币"]')))
                treasure2.click()
                time.sleep(22)

                treasure3 = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@text="继续退出"]')))
                treasure3.click()
                print('结束返回')
            except:
                # 返回
                self.driver.press_keycode(4)
                print('未结束返回')
        print('结束')

    # 入口函数u
    def main(self):
        print('打开 {} App ...'.format('视频'))
        #func_list = ['smallvideo', 'hshb']  # 新闻阅读，小视频，视频三个方法随机
        func_list = ['smallvideo']
        for i in range(12):
            exp = 'self.{}({})'.format(random.choice(func_list), random.randint(9000, 10500))
            print(exp)
            eval(exp)
            print('App {} 今日任务完成！'.format('视频'))
            #home键
            self.driver.press_keycode(3)
            self.driver.close()

#kuaishou jisuban
qutoutiao_appPackage = 'com.kuaishou.nebula'
qutoutiao_appActivity = 'com.yxcorp.gifshow.HomeActivity'

#douyin jisuban
# qutoutiao_appPackage = 'com.ss.android.ugc.aweme.lite'
# qutoutiao_appActivity = 'com.ss.android.ugc.aweme.splash.SplashActivity'

#huoshan jisuban
# qutoutiao_appPackage = 'com.ss.android.ugc.livelite'
# qutoutiao_appActivity = 'com.ss.android.ugc.live.main.MainActivity'

#shuabao
# qutoutiao_appPackage = 'com.jm.video'
# qutoutiao_appActivity = 'com.jm.video.ui.main.SplashActivity'


#weishi
# qutoutiao_appPackage = 'com.tencent.weishi'
# qutoutiao_appActivity = 'com.tencent.oscar.module.splash.SplashActivity'


if __name__ == '__main__':
    qutoutiao = Qutoutiao(qutoutiao_appPackage, qutoutiao_appActivity)
    qutoutiao.main()